package com.kwan.xframe.view_model;

import android.app.Application;

import com.kwan.xframe.util.RxLifeUtils;
import com.trello.rxlifecycle4.LifecycleTransformer;

import org.reactivestreams.Subscription;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Action;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.subscribers.DisposableSubscriber;
import timber.log.Timber;

public abstract class BaseViewModel extends AndroidViewModel implements LifecycleEventObserver {

	/**
	 * 管理RxJava，主要针对RxJava异步操作造成的内存泄漏
	 */
	private CompositeDisposable mCompositeDisposable;

	public BaseViewModel(@NonNull Application application) {
		super(application);
		mCompositeDisposable = new CompositeDisposable();
	}


	protected void addSubscribe(Disposable disposable) {
		if (mCompositeDisposable == null) {
			mCompositeDisposable = new CompositeDisposable();
		}
		mCompositeDisposable.add(disposable);
	}

	@Override
	public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
		Timber.e("%s onStateChanged : %s", this.getClass().getSimpleName(), event.toString());


	}

	//ViewModel清除时回调
	@Override
	protected void onCleared() {
		super.onCleared();
		Timber.e("%s onCleared....", this.getClass().getSimpleName());
		//ViewModel销毁时会执行，同时取消所有异步任务
		if (mCompositeDisposable != null) {
			mCompositeDisposable.clear();
		}
	}

	public <T> void execute(Flowable<T> command, DisposableSubscriber<T> callBack) {
		addSubscribe(command
				.doOnSubscribe(new Consumer<Subscription>() {
					@Override
					public void accept(Subscription subscription) throws Throwable {
						Timber.e("execute doOnSubscribe...");
					}
				})
				.doOnComplete(new Action() {
					@Override
					public void run() throws Throwable {
						Timber.e("execute doOnComplete...");
					}
				})
				//线程调度
				.compose(RxLifeUtils.schedulersTransformer())
				//生命周期
				//.compose(getLifecycleTransformer())
				.subscribeWith(callBack));
	}


	/**
	 * 使用RXLife 在指定的 生命周期 取消
	 *
	 * @param command 要执行操作
	 * @param callBack 回调
	 * @param lifecycleTransformer 什么周期 比如 bindUntilEvent(ActivityEvent.PAUSE)
	 * @param <T> 数据类型
	 */

	public <T> void execute(Flowable<T> command,
							DisposableSubscriber<T> callBack,
							LifecycleTransformer<T> lifecycleTransformer) {

		addSubscribe(command
				.doOnSubscribe(new Consumer<Subscription>() {
					@Override
					public void accept(Subscription subscription) throws Throwable {
						Timber.e("execute doOnSubscribe...");
					}
				})
				.doOnComplete(new Action() {
					@Override
					public void run() throws Throwable {
						Timber.e("execute doOnComplete...");
					}
				})
				//线程调度
				.compose(RxLifeUtils.schedulersTransformer())
				//生命周期
				.compose(lifecycleTransformer)
				.subscribeWith(callBack));
	}


}
